package com.jaws.demo.schedule.quartz;

import com.jaws.core.common.log.Log;
import org.quartz.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * quartz工具类
 * 
 * @author maojian
 * @date 2017-09-21 11:36:01
 * @since v1.0.0
 */
public class QuartzTaskUtils {

	private static Logger logger = LoggerFactory.getLogger(QuartzTaskUtils.class);

	public static DynamicJob createJob(String cornEx, String jobName, Class<Job> job, boolean isDuplicate) {
		final String OP = "QuartzTaskUtils.createJob";
		
		DynamicJob dynamicJob = new DynamicJob();
		dynamicJob.cronExpression(cornEx);
		dynamicJob.target(job);
		dynamicJob.jobName(jobName);
		try {
//			if (isDuplicate) {
//				// 如果重复下单，则更新调度机制
//				DynamicSchedulerFactory.updateJob(dynamicJob);
//			} else {
//				DynamicSchedulerFactory.registerJob(dynamicJob);
//			}
			
			DynamicSchedulerFactory.registerOrUpdateJob(dynamicJob);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalStateException(e);
		} finally {
			logger.info(Log.op(OP).msg("【添加异步通知任务】{}").kv("job name", job.getName()).toString());
		}
		return dynamicJob;
	}

}
